package com.mazaiting.instructions.conversions;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;

/**
 * d2x系列指令把double变量强制转换成其他类型
 *
 * @author mazaiting
 * @since 2021/8/15
 */
public class D2xInstruction {

    /**
     * 双精度转换为单精度类型
     */
    public static class D2F extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            double v1 = stack.popDouble();
            float v2 = (float) v1;
            stack.pushFloat(v2);
        }
    }

    /**
     * 双精度转换为整型
     */
    public static class D2I extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            double v1 = stack.popDouble();
            int v2 = (int) v1;
            stack.pushInt(v2);
        }
    }


    /**
     * 双精度转换为长整型
     */
    public static class D2L extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            double v1 = stack.popDouble();
            long v2 = (long) v1;
            stack.pushLong(v2);
        }
    }


}
